# -*- coding:utf-8 -*-

import discussDao, json, memcache, os, time, hashlib
os.sys.path.append('../comment/')
import commentLogic
import os
os.sys.path.append('../../utils/constants/')
from constants import *

class discussLogic(object):
    "discuss: let me tell u sth about storage-visiting"
    def __init__(self, mysql_conn, memcache_conn):
        self.__dao = discussDao.discussDao(mysql_conn)
        self.__mc = memcache_conn
        self.__mc_expire_time = 10*86400

        dict_comment_table = {}
        dict_comment_table['thread_id_keyname'] = 'discuss_id'
        dict_comment_table['thread_table'] = 'discuss'
        dict_comment_table['comment_table'] = 'discuss_comment'
        dict_comment_table['comment_id_table'] = 'discuss_comment_id'
        self.__commentLogic = commentLogic.commentLogic(dict_comment_table, mysql_conn, memcache_conn)

    def remove(self, delete_item):
        return self.__dao.remove(delete_item)

    def add(self, discuss_item):
        return self.__dao.add(discuss_item)

    def mod(self, discuss_item):
        return self.__dao.mod(discuss_item)

    def get_discuss(self, discuss_id, uip):
        ret = {}
        ret['retcode'] = 0
        ret['message'] = "OK"
        discuss_item = self.__dao.get_discuss(discuss_id)
        if discuss_item and 0 != discuss_item['status']:
            ret['retcode'] = E_THREAD_REMOVED
            ret['message'] = 'discuss has removed'
        else:
            pv_item = {}
            pv_item['discuss_id'] = discuss_id
            pv_item['uip'] = uip
            self.add_pv_count(pv_item)
            
            ret['retbody'] = {}
            ret['retbody']['discuss'] = discuss_item
            cmtlist = self.get_comment(discuss_id, 0)
            if 0 == cmtlist['retcode']:
                ret['retbody']['cmtlist'] = cmtlist['retbody']
            else:
                ret['retbody']['cmtlist'] = []
        return ret

    def ding(self, ding_item):
        # ding_item has 'discuss_id', 'uip', 'ding'
        ret = {}
        ret['retcode'] = 0
        ret['message'] = "OK"
        # 检查合法性
        if not ding_item.has_key('discuss_id') or not ding_item.has_key('uip'):
            ret['retcode'] = E_KEY_NOT_EXIST
            ret['message'] = "ding_item field [%s] or [%s] not exist" % ('discuss_id', 'uip', )
            return ret
        
        key = 'ding:discuss:%u:%s' % (ding_item['discuss_id'], ding_item['uip'],)
        md5str = hashlib.md5(key).hexdigest()
        mckey = md5str[:16]
        if not self.__mc.get(mckey):
            self.__mc.add(mckey, int(time.time()), self.__mc_expire_time)
            return self.__dao.ding(ding_item)
        else:
            ret = {}
            ret['retcode'] = E_DUP_OPERATION
            ret['message'] = 'ding duplicate operation table[%s] uip[%s] discuss_id[%u].' %\
                    ('discuss', ding_item['uip'], ding_item['discuss_id'],)
            return ret

    def add_pv_count(self, pv_item):
        # pv_item has 'discuss_id', 'uip',
        # ip一小时内pv去重
        ret = {}
        ret['retcode'] = 0
        ret['message'] = "OK"
        # 检查合法性
        if not pv_item.has_key('discuss_id') or not pv_item.has_key('uip'):
            ret['retcode'] = E_KEY_NOT_EXIST
            ret['message'] = "pv_item field [%s] or [%s] not exist" % ('discuss_id', 'uip', )
            return ret
        key = 'pv:discuss:%u:%s:%s' % (pv_item['discuss_id'], pv_item['uip'], time.strftime("%Y%m%H"), )
        md5str = hashlib.md5(key).hexdigest()
        mckey = md5str[:16]
        if not self.__mc.get(mckey):
            self.__mc.add(mckey, int(time.time()), self.__mc_expire_time)
            return self.__dao.add_pv_count(pv_item['discuss_id'])
        else:
            ret = {}
            ret['retcode'] = E_DUP_OPERATION
            ret['message'] = 'pv duplicate operation table[%s] uip[%s] discuss_id[%u].' %\
                    ('discuss', pv_item['uip'], pv_item['discuss_id'],),
            return ret

    def order_by(self, field, pageno, desc = True):
        ret = {}
        ret['retcode'] = 0
        ret['retbody'] = self.__dao.order_by(field, pageno, desc)
        return ret

    def ding_comment(self, ding_item):
        # ding_item has 'comment_id', 'op_uid', 'ding'
        return self.__commentLogic.ding_comment(ding_item)
    def add_comment(self, comment_item):
        return self.__commentLogic.add_comment(comment_item)
    def del_comment(self, comment_item,):
        return self.__commentLogic.del_comment(comment_item)
    def get_comment(self, thread_id, pageno):
        return self.__commentLogic.get_comment(thread_id, pageno)
